package com.lhd.web;

import com.lhd.po.Blog;
import com.lhd.po.Tag;
import com.lhd.service.BlogService;
import com.lhd.service.TagService;
import com.lhd.service.TypeService;
import com.lhd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhandong on 2022/4/20.
 */
@RestController
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/getTag/{page}")
    public Map getTag(@PathVariable Long page){
        Map<String,Object> map = new HashMap<>();
        Integer count = tagService.countTag();
        Integer rem = Math.toIntExact(count % 10);
        List<Tag> tags = tagService.listTagByPage(page);
//        List<Type> types = typeService.listType();
        map.put("tags",tags);
        map.put("preBoolean",page > 0);
        map.put("nexBoolean",page < ((count/10 + (rem == 0 ? 0 : 1))-1));
        map.put("curPage",page);
        return map;
    }

    @GetMapping("/tags/{id}")
    public Map tags(@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                    @PathVariable Long id) {

        //所有标签全部展示，同时第一个标签点亮，同时拿出第一个标签对应的前
        //id：标签id   -1表示首页，咋默认使用首个tag的id
        //page：页码   一页设置3篇博客

        //获取所有的tag
        List<Tag> tags = tagService.listTag();
        //获取所有tag对应的博客数目
        for (Tag tag:tags){
            tag.setCount(tagService.getCountByTagId(tag.getId()));
        }
        //如果id为-1，且tag数目＞0则将第一个tagid置为激活
        if (id == -1 && tags.size() > 0) {
            id = tags.get(0).getId();
        }
        //获取要展示的Blog
        List<Blog> blogs = blogService.listBlog(id, page);
        for (Blog b:blogs){
            if (b.getUserId() == null){
                b.setUser(userService.getUserById(1L));
            }else {
                b.setUser(userService.getUserById(b.getUserId()));
            }
            b.setType(typeService.getType(b.getTypeId()));
            b.setTags(tagService.getTagsByBlogId(Math.toIntExact(b.getId())));
        }
        Map<String,Object> map = new HashMap<>();
        map.put("tags", tags);
        map.put("page", blogs);
        map.put("activeTagId", id);
        Long count = tagService.getCountByTagId(id);
        Integer rem = Math.toIntExact(count % 3);
        map.put("pageNum",count/3 + (rem == 0 ? 0 : 1));
        map.put("curPage",page);
        Boolean preBoolean = page > 0;
        Boolean nexBoolean = page < ((count/3 + (rem == 0 ? 0 : 1))-1);
        map.put("preBoolean",preBoolean);
        map.put("nexBoolean",nexBoolean);
        return map;
    }
}
